<style>
	page {
		background-color: white;
	}
</style>
<style lang="less" scoped>
	.logo {
		width: 100%;
		height: 40vh;

		image {
			width: 338rpx;
			height: 140rpx;
		}
	}

	.btn {
		margin-top: 60rpx;
		width: 650rpx;
		height: 88rpx;
		opacity: 1;
		background: #09bb07;
		border-radius: 12rpx;
		box-shadow: 0rpx 6rpx 20rpx 0rpx rgba(0, 0, 0, 0.14);
		color: #fff;
	}

	.btn:after {
		border: none;
	}
</style>

<template>
	<view class="content">
		<view class="center-flex logo">
			<image src="../../static/images/logo.png" mode=""></image>
		</view>

		<button class="btn cfff f32 flex-center" type="default" @click="login">
			<u-icon margin-right="15" name="weixin-fill" color="#fff" size="38"></u-icon><span class="p-left10">微信登录</span>
		</button>



		<official-account style="width: 750rpx;height:80rpx;position: fixed;bottom:70rpx;" bindload="">
		</official-account>
	</view>
</template>

<script>
import { Set_BaseStorage } from '../../common';
	export default {
		data() {
			return {
				canIUseGetUserProfile: false,
			};
		},
		onLoad() {
			if (wx.getUserProfile) {

				this.canIUseGetUserProfile = true


			}
		},
		methods: {

			login() {
				if (!uni.getStorageSync('userinfo')) {
					wx.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							console.log(res)
							var userinfo = res.userInfo
							uni.setStorageSync('userinfo', userinfo)
							this.tologin(userinfo)
						}
					})
				} else {
					var userinfo = uni.getStorageSync('userinfo')
					this.tologin(userinfo)
				}

			},
			tologin(userinfo) {
				uni.showLoading({
					title: '登录中...',
					mask: true
				})
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log('loginRes', loginRes)
						var code = loginRes.code;
						// return
						this.$pop.ajaxPost('/firstLoginWeChat', {
							code,
							headPortrait: userinfo.avatarUrl
						}).then(res => {
							console.log('登录')
							if (res.code == 10) {
								this.$go('/pages/login/bindphone')
							} else if (res.code == 0) {
								Set_BaseStorage(res)
								getApp().getTeam()
								getApp().getdic()
								getApp().getunit()
								getApp().getuser()
								uni.hideLoading()
								setTimeout(() => {
									wx.navigateBack()
								}, 1000)
								uni.$emit('login')


							} else {
								uni.showModal({
									title: res.msg
								})
							}
						})
					}
				})
			}

		}
	};
</script>
